<?php
namespace app\system\controller;

use app\common\model\CnzzModel;
use org\net\Cnzz;
use think\helper\Time;
use think\Request;

class CnzzController extends BaseController
{

    private $cnzz;
    private $cnzzModel;
    private $islogin;
    /**
     * 初始化
     * AreaController constructor.
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->assign('tabPage',$this->request->action());
        $this->cnzzModel  = new CnzzModel();
        $info = $this->cnzzModel->where(['site_id'=>$this->getSiteId(),'is_active'=>1])->find();
        if($info){
            $this->islogin=1;
            $this->cnzz = new Cnzz($info['cnzz_site_id'],$info['cnzz_view_pass']);
        }else{
            $this->islogin=0;
        }
    }

    /**
     * 检测登陆
     */
    private function checkLogin(){
        if($this->islogin==0){
            $this->redirect('Cnzz/cnzzActive');
        }
    }

    /*
     * 初始化
     */
    public function cnzzActive(){
        if($this->request->isAjax()){
            $data = $this->request->param();
            if($this->cnzzModel->where('site_id',$this->getSiteId())->find()){
                $cnzz['cnzz_site_id'] = $data['cnzz_site_id'];
                $cnzz['cnzz_view_pass'] = $data['cnzz_view_pass'];
                $this->cnzzModel->where('site_id',$this->getSiteId())->update($cnzz);
            }else{
                $data['site_id'] = $this->getSiteId();
                $this->cnzzModel->addData($data);
            }
            if($this->cnzzModel->activation($this->getSiteId(),$data['cnzz_site_id'],$data['cnzz_view_pass'])){
                $this->success('配置成功',url('Cnzz/siteOverview'));
            }else{
                $this->error($this->cnzzModel->getError());
            }
        }
        $info = $this->cnzzModel->where('site_id',$this->getSiteId())->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
    /**
     * 网站概况
     * @return mixed
     */
    public function siteOverview(){
        $this->checkLogin();
        $static = $this->cnzz->siteOverview();
        $list = $static['data']['summary']['items'];
        unset($list[7]);
        unset($list[8]);
        $tableTile = [1=>'今日',2=>'昨日',3=>'今日预计',4=>'昨日此时',5=>'近90日平均',6=>'历史最高',7=>'历史累计'];
        $this->assign('tableTile',$tableTile);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function AjaxflowTrendLine(){
        $Period = 'Hour';
        $type = $this->request->param('type',1);
        if($type==2){  //昨日
            $yesterday = Time::yesterday();
            $st = date('Y-m-d',$yesterday[0]);
            $et = date('Y-m-d',$yesterday[1]);
        }
        if($type==4){  //最近7日
            $Period = 'Day';
            $day = Time:: daysAgo(7);
            $st = date('Y-m-d',$day);
            $et = date('Y-m-d');
        }
        if($type==5){  //最近30日
            $Period = 'Day';
            $day = Time:: daysAgo(30);
            $st = date('Y-m-d',$day);
            $et = date('Y-m-d');
        }
        $flow = $this->cnzz->flowTrend($Period,$st,$et);
        $flow = $flow['data']['flash'];
        $timeFlow = $flow['data'][0];
        if($Period=='Day'){
            $timeFlow = $flow['data'];
        }
        $timeData = [];
        foreach($timeFlow as $v){
            $timeData[0][] = empty($v[0][0])?0:$v[0][0];
            $timeData[1][] = empty($v[1][0])?0:$v[1][0];
            $timeData[2][] = empty($v[2][0])?0:$v[2][0];
        }
        if($Period=='Day'){
            $first = strtotime($st);
            for($i=$first;$i<time();$i+=60*60*24){
                $time[]=date('Ymd',$i);
            }
        }else{
            for($i=0;$i<=23;$i++){
                $time[]=$i.':00-'.$i.':59';
            }
        }

        for($i=0;$i<3;$i++){
            $series[]=[
                'name'=>$flow['title'][$i],
                'type'=>'line',
                'data'=>$timeData[$i],
                'markPoint'=>['data'=>[['type'=>'max','name'=>'最大值'],['type'=>'min','name'=>'最小值']]],
                'markLine'=>['data'=>[['type'=>'average','name'=>'平均值']]]
            ];
        }
        $flowData = [
            'title'=>['text'=>'流量趋势统计'],
            'legend'=>['data'=>$flow['title']],
            'xAxis'=>['data'=>$time],
            'series'=>$series
        ];
        return $flowData;
    }
    /**
     * 搜索引擎
     * @return mixed
     */
    public function trafSearch(){
        $this->checkLogin();
        return $this->fetch();
    }
    public function ajaxtrafSearchPie(){
        //trafSearch($type='Pie',$Quota='pv',$Period='Hour',$st='',$et=''){
        $Period = 'Hour';
        $type = $this->request->param('type',1);
        if($type==2){  //昨日
            $yesterday = Time::yesterday();
            $st = date('Y-m-d',$yesterday[0]);
            $et = date('Y-m-d',$yesterday[1]);
        }
        if($type==4){  //最近7日
            $Period = 'Day';
            $day = Time:: daysAgo(7);
            $st = date('Y-m-d',$day);
            $et = date('Y-m-d');
        }
        if($type==5){  //最近30日
            $Period = 'Day';
            $day = Time:: daysAgo(30);
            $st = date('Y-m-d',$day);
            $et = date('Y-m-d');
        }
        $searchData = $this->cnzz->trafSearch('Pie','pv',$Period,$st,$et);
        $searchData = $searchData['data']['flash'];
        $index = (count($searchData['data'][0])-1);
        $seriesData = [];
        for($i=0;$i<=$index;$i++){
            $seriesData[]=['value'=>$searchData['data'][0][$i],'name'=>$searchData['title'][0][$i]];
        }
        $flowData = [
            'title'=>['text'=>'站点访问来源'],
            'legend'=>['data'=>$searchData['title'][0]],
            'series'=>['data'=>$seriesData]
        ];
        return $flowData;
    }
    public function AjaxTrafSearchLine(){
        $Period = 'Hour';
        $type = $this->request->param('type',1);
        if($type==2){  //昨日
            $yesterday = Time::yesterday();
            $st = date('Y-m-d',$yesterday[0]);
            $et = date('Y-m-d',$yesterday[1]);
        }
        if($type==4){  //最近7日
            $Period = 'Day';
            $day = Time:: daysAgo(7);
            $st = date('Y-m-d',$day);
            $et = date('Y-m-d');
        }
        if($type==5){  //最近30日
            $Period = 'Day';
            $day = Time:: daysAgo(30);
            $st = date('Y-m-d',$day);
            $et = date('Y-m-d');
        }
        $searchData = $this->cnzz->trafSearch('Line','pv',$Period,$st,$et);
        $searchData = $searchData['data']['flash'];
        if($Period=='Day'){
            $timeFlow = $searchData['data'];
            $timeFlow2=[];
            foreach ($timeFlow as $v){
                $timeFlow2[]=$v[0];
            }
            $timeData = [];
            foreach($timeFlow2 as $v){
                $timeData[0][] = empty($v[0])?0:$v[0];
                $timeData[1][] = empty($v[1])?0:$v[1];
                $timeData[2][] = empty($v[2])?0:$v[2];
            }
        }else{
            $timeFlow = $searchData['data'][0];
            $timeFlow2=[];
            foreach ($timeFlow as $v){
                $timeFlow2[]=$v[0];
            }
            $timeData = [];
            foreach($timeFlow2 as $v){
                $timeData[0][] = empty($v[0])?0:$v[0];
                $timeData[1][] = empty($v[1])?0:$v[1];
                $timeData[2][] = empty($v[2])?0:$v[2];
            }
        }
        if($Period=='Day'){
            $time=$searchData['date'];
        }else{
            for($i=0;$i<=23;$i++){
                $time[]=$i.':00-'.$i.':59';
            }
        }
        for($i=0;$i<3;$i++){
            $series[]=[
                'name'=>$searchData['title'][$i],
                'type'=>'line',
                'data'=>$timeData[$i],
                /*  'markPoint'=>['data'=>[['type'=>'max','name'=>'最大值'],['type'=>'min','name'=>'最小值']]],
                  'markLine'=>['data'=>[['type'=>'average','name'=>'平均值']]]*/
            ];
        }
        $flowData = [
            'title'=>['text'=>'搜索引擎'],
            'legend'=>['data'=>$searchData['title']],
            'xAxis'=>['data'=>$time],
            'series'=>$series
        ];
        return $flowData;
    }
    /**
     * 访问来源
     * @return mixed
     */
    public function sourceChart(){
        $this->checkLogin();
        return $this->fetch();
    }
    /**
     * 网站来源数据
     * @return array
     */
    public function AjaxsourceChart(){
        $Period = 'Hour';
        $type = $this->request->param('type',1);
        if($type==2){  //昨日
            $yesterday = Time::yesterday();
            $st = date('Y-m-d',$yesterday[0]);
            $et = date('Y-m-d',$yesterday[1]);
        }
        if($type==4){  //最近7日
            $Period = 'Day';
            $day = Time:: daysAgo(7);
            $st = date('Y-m-d',$day);
            $et = date('Y-m-d');
        }
        if($type==5){  //最近30日
            $Period = 'Day';
            $day = Time:: daysAgo(30);
            $st = date('Y-m-d',$day);
            $et = date('Y-m-d');
        }
        $a=['Line','Pie'];
        foreach ($a as $v){
            $flow[] = $this->cnzz->sourceChart($Period,$v,$st,$et);
        }
        $flow[0] = $flow[0]['data']['flash'];
        $timeFlow = $flow[0]['data'][0];
        if($Period=='Day'){
            $timeFlow = $flow[0]['data'];
        }
        $timeData = [];
        foreach($timeFlow as $v){
            $timeData[0][] = empty($v[0][0])?0:$v[0][0];
            $timeData[1][] = empty($v[0][1])?0:$v[0][1];
            $timeData[2][] = empty($v[0][2])?0:$v[0][2];
            $timeData[3][] = empty($v[0][3])?0:$v[0][3];
        }
        if($Period=='Day'){
            $first = strtotime($st);
            for($i=$first;$i<time();$i+=60*60*24){
                $time[]=date('Ymd',$i);
            }
        }else{
            for($i=0;$i<=23;$i++){
                $time[]=$i.':00-'.$i.':59';
            }
        }
        for($i=0;$i<4;$i++){
            $series[]=[
                'name'=>$flow[0]['title'][$i],
                'type'=>'line',
                'data'=>$timeData[$i],
                'markPoint'=>['data'=>[['type'=>'max','name'=>'最大值'],['type'=>'min','name'=>'最小值']]],
                'markLine'=>['data'=>[['type'=>'average','name'=>'平均值']]]
            ];
        }
        $flowData = [
            'title'=>['text'=>'网站访问来源分类'],
            'legend'=>['data'=>$flow[0]['title']],
            'xAxis'=>['data'=>$time],
            'series'=>$series
        ];
        $piedata = [];
        foreach ($flow[1]['data']['flash']['data'][0] as $k=>$v){
            $piedata[] = ['value'=>$v,'name'=>$flow[1]['data']['flash']['title'][0][$k]];
        }
        $pieData = [
            'title' => ['text'=>'网站访问总量'],
            'legend' => [
                'data'=>$flow[1]['data']['flash']['title'][0]
            ],
            'series' => [
                'data'=>$piedata,
            ],
        ];
        $result[0] = $flowData;
        $result[1] = $pieData;
        return $result;
    }
    /**
     * seo推荐
     * @return mixed
     */
    public function seo(){
        $this->checkLogin();
        return $this->fetch();
    }

    public function ajaxSeo(){
        $module = $this->request->param('module','newlist');
        $page = $this->request->param('page',1);
        $day = $this->request->param('day',1);
        $time = Time::daysAgo($day);
        $day     = date('Y-m-d',$time);
        $result = $this->cnzz->seo($module,$page,$day,$day);
        return $result['data'][$module];
    }

    /**
     * 最近15分钟分析
     * @return mixed
     */
    public function flowRealtime(){
        $this->checkLogin();
        return $this->fetch();
    }
    public function ajaxfowRealtime(){
        $searchData = $this->cnzz->flowRealtime('Pie');
        $static = $searchData['data']['flash']['data'];
        $data = [
            'time' =>date('H:i',strtotime($searchData['data']['time'][1])).'—'.date('H:i',strtotime($searchData['data']['time'][0])),
            'high' =>$static['High'],
            'high_time' =>$static['Time'],
            'total_ip' =>$static['total_ip'],
            'total_pv' =>$static['total_pv'],
            'total_uv' =>$static['total_uv'],
        ];
        return $data;
    }
    public function ajaxflowRealtimeLine(){
        $searchData = $this->cnzz->flowRealtime('Line');
        $searchData = $searchData['data']['flash'];
        $timeFlow = $searchData['data'];
        $timeData = [];
        foreach($timeFlow as $v){
            $timeData[0][] = empty($v[0][0])?0:$v[0][0];
            $timeData[1][] = empty($v[1][0])?0:$v[1][0];
            $timeData[2][] = empty($v[2][0])?0:$v[2][0];
        }
        for($i=0;$i<3;$i++){
            $series[]=[
                'name'=>$searchData['title'][$i],
                'type'=>'line',
                'data'=>$timeData[$i],
                'markPoint'=>['data'=>[['type'=>'max','name'=>'最大值'],['type'=>'min','name'=>'最小值']]],
                'markLine'=>['data'=>[['type'=>'average','name'=>'平均值']]]
            ];
        }
        $flowData = [
            'title'=>['text'=>'最近15分钟趋势图'],
            'legend'=>['data'=>$searchData['title']],
            'xAxis'=>['data'=>$searchData['date']],
            'series'=>$series
        ];
        return $flowData;
    }

}
